export type FizzBuzzType = number | string;
const complementation = (num:number, words:{[key: string]: string}):string => {
  let result = ''
  for (const key in words) {
    if (Object.prototype.hasOwnProperty.call(words, key)) {
      const numKey = Number(key)
      if (!(num % numKey)) {
        result += words[numKey] || ''
      }
    }
  }
  return result
}
const throwErr = (str:string) => {
  throw new RangeError(str)
}
const fizzBuzzConverter = (number: number): FizzBuzzType => {
  // Fizz---3
  // Buzz---5
  // Whizz---7

  // 357----xxx

  // include 3---Fizz

  // include 5---Fizz


  if (!number && number !== 0) {
    return throwErr('参数不能为空')
  }
  if (typeof number !== 'number') {
    return throwErr('参数只能为数字')
  }
  // if (number < 1 || number > 100) {
  //   return throwErr('参数1<=number<=100')
  // }
  let result = ''
  const numToStr = number.toString()
  const arr = numToStr.split('')
  const words = {
    3: 'Fizz',
    5: 'Buzz',
    7: 'Whizz'
  }
  let has5 = false
  let has7= false
  if (arr.indexOf('7') > -1) {
    has7 = true
    if (arr.indexOf('5') > -1) {
      has5 = true
      const newWords = {
        3: 'Fizz',
        7: 'Whizz'
      }
      result = complementation(number, newWords)
    } else {
      const newWords = {
        3: 'Fizz',
        7: 'Whizz'
      }
      result = complementation(number, newWords)
    }
  }
  if (arr.indexOf('5') > -1 && !has7) {
    has5 = true
    const newWords = {
      5: 'Buzz',
      7: 'Whizz'
    }
    result = complementation(number, newWords)
  } else {
    if (!result) {
      console.log(arr)
      // num包含3
      for (const item of arr) {
        result += (item === '3' && words[item]) || ''
      }
    }
  }

  // 不包含3
  if (!result && !has5 && !has7) {
    result = complementation(number, words)
  }
  if (!result) {
    result = numToStr
  }
  return result
}

export default fizzBuzzConverter;